﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LEARNREN.COM.UI;
using LEARNREN.COM.Entity;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity.Enum;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.WebSite
{
    public partial class ShowProject : BasePage
    {
        protected IEnumerable<Catalogue> productCatalogueList = null;
        protected IEnumerable<Catalogue> childCatalogueList = null;
        protected Catalogue curentCatalogue = null;
        protected Project curProject = null;

        private ProjectLogic projectLogic = null;
        private CatalogueLogic catalogueLogic = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            Initialize();
            InitializeContent();
        }

        private void Initialize()
        {
            productCatalogueList = catalogueLogic.GetConfigedProductCatalogueCollection();

            string sid = Request.QueryString["id"];
            long id = 0;
            if (long.TryParse(sid, out id))
            {
                curProject = projectLogic.GetById(id);
                if (curProject == null)
                {
                    Response.Redirect("~/");
                }

                curentCatalogue = catalogueLogic.GetByIdFromCache(curProject.CatalogueId);
                if (curentCatalogue == null)
                {
                    Response.Redirect("~/");
                }

                childCatalogueList = catalogueLogic.GetChildren(curentCatalogue.Id);
            }
            else
            {
                Response.Redirect("~/");
            }
        }

        private void InitializeContent()
        {
            LiteralProjectName.Text = curProject.ProjectName;
            LiteralProjectDate.Text = curProject.ProjectDate.ToShortDateString();
            LiteralContent.Text = curProject.ProjectDiscription;

            this.LinkProductImage.HRef = string.IsNullOrEmpty(curProject.ProjectImage) ? "" : VirtualPathUtility.ToAbsolute(curProject.ProjectImage);
            this.LinkProductImage.Title = curProject.ProjectName;
            this.ImageProductIco.Src = string.IsNullOrEmpty(curProject.ProjectImage) ? "" : VirtualPathUtility.ToAbsolute(curProject.ProjectImage);
            this.ImageProductIco.Alt = curProject.ProjectName;

            this.HyperLinkCunnectUs.NavigateUrl = string.Format("~/FeedBackUs.aspx?MsgTitle={0}&id={1}", Server.UrlEncode(string.Format("案例咨询：{0}", curProject.ProjectName)), UtilityTool.GetConfiguration("feedbackCatalogueId"));
        }

        protected override void InitLogic()
        {
            projectLogic = new ProjectLogic();
            catalogueLogic = new CatalogueLogic();
        }

        protected override void DisposeLogic()
        {
            if (projectLogic != null)
            {
                projectLogic.Dispose();
                projectLogic = null;
            }
            if (catalogueLogic != null)
            {
                catalogueLogic.Dispose();
                catalogueLogic = null;
            }
        }
    }
}